package org.meerkat.forum.client;

import org.meerkat.forum.client.maintree.MainTreePanel;
import org.meerkat.forum.client.threadslist.ThreadsList;
import org.meerkat.forum.client.threadview.ThreadView;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;

public class Application extends Viewport {

	private static Application instance = null;

	private ContentPanel southPanel;

	protected Application() {
	}

	public static Application getInstance() {
		if (instance == null) {
			instance = new Application();
			instance.init();
		}
		return instance;
	}

	protected void init() {
		BorderLayout layout = new BorderLayout();
		this.setLayout(layout);

		createWestPanel();
		createSouthPanel();
		createCenterPanel();
	}

	private void createCenterPanel() {
		RowLayout layout = new RowLayout(Orientation.HORIZONTAL);
		LayoutContainer container = new LayoutContainer(layout);
		container.setStyleAttribute("background-color", "white");

		ThreadsList threadsList = new ThreadsList("Threads list");
		container.add(threadsList.getView(), new RowData(0.5, 1, new Margins(5)));

		ThreadView threadView = new ThreadView();
		container.add(threadView, new RowData(0.5, 1, new Margins(5)));

		add(container, new BorderLayoutData(LayoutRegion.CENTER));
	}

	private void createSouthPanel() {
		southPanel = new ContentPanel(new FlowLayout());
		southPanel.setHeading("Views");

		BorderLayoutData layoutData = new BorderLayoutData(LayoutRegion.SOUTH);
		layoutData.setSplit(true);
		layoutData.setSize(60);
		layoutData.setCollapsible(true);
		layoutData.setFloatable(true);

		add(southPanel, layoutData);
	}

	private void createWestPanel() {
		LayoutContainer container = new LayoutContainer();
		RowLayout layout = new RowLayout(Orientation.VERTICAL);
		container.setLayout(layout);

		BorderLayoutData layoutData = new BorderLayoutData(LayoutRegion.WEST,
				100);
		layoutData.setSplit(true);
		layoutData.setMinSize(100);

		add(container, layoutData);

		// Add logo
		container.add(new Logo(), new RowData(1, 50));

		// Add Tree
		MainTreePanel treePanel = MainTreePanel.getInstance();
		container.add(treePanel, new RowData(1, 1));
	}

	/**
	 * @return the southPanel
	 */
	public ContentPanel getSouthPanel() {
		return southPanel;
	}
}
